package GameServer

import (
	. "PBMessage"
	. "SGNet"
	"github.com/golang/protobuf/proto"
	//log "misc/seelog"
	"time"
)

const (
	READYROOM_TIME = 20
)

type ReadyRoom struct {
	RoomID         int64
	clientPlayers  []*ClientPlayer
	roomPlayers    []*ReadRoomPlayer
	StartReadyTime int64
	IsStart        bool
	RoomSize       int
}

func NewReadyRoom(players []*ClientPlayer) *ReadyRoom {
	room := &ReadyRoom{}
	room.IsStart = false
	room.RoomID = time.Now().UnixNano()
	room.StartReadyTime = time.Now().UnixNano()
	room.RoomSize = GetGameHall().RoomSize
	room.clientPlayers = players
	room.roomPlayers = make([]*ReadRoomPlayer, len(players))
	for ci := 0; ci < len(players); ci++ {

		players[ci].PlayerState = PLAYERSTATE_ReadyRoom
		players[ci].ReadyRoomID = room.RoomID
		players[ci].Sess.SetAliveTime(int64(time.Second * 60))

		newReadyplayers := &ReadRoomPlayer{}
		room.roomPlayers[ci] = newReadyplayers
		newReadyplayers.UserName = proto.String(players[ci].UserName)
		newReadyplayers.IsReady = proto.Bool(false)
		newReadyplayers.SelectId = proto.String("")
		if ci < room.RoomSize/2 {
			newReadyplayers.Camp = proto.Int32(0)
		} else {
			newReadyplayers.Camp = proto.Int32(1)
		}
	}

	return room
}

func (this *ReadyRoom) ToClient() {
	//SendToCamp0
	enterRoomCamp0 := &SC_EnterReadyRoom{}
	enterRoomCamp0.RoomID = proto.Int64(this.RoomID)
	enterRoomCamp0.Camp = proto.Int32(0)
	enterRoomCamp0.PlayerList = this.roomPlayers[:this.RoomSize/2]
	for camp0i := 0; camp0i < this.RoomSize/2; camp0i++ {
		this.clientPlayers[camp0i].Sess.SendPacket(PBID_SC_EnterReadyRoom, enterRoomCamp0)
	}
	//SendToCamp1
	enterRoomCamp1 := &SC_EnterReadyRoom{}
	enterRoomCamp1.RoomID = proto.Int64(this.RoomID)
	enterRoomCamp1.Camp = proto.Int32(1)
	enterRoomCamp1.PlayerList = this.roomPlayers[this.RoomSize/2:]
	for camp1i := this.RoomSize / 2; camp1i < this.RoomSize; camp1i++ {
		this.clientPlayers[camp1i].Sess.SendPacket(PBID_SC_EnterReadyRoom, enterRoomCamp1)
	}
}
func (this *ReadyRoom) GetPlayer(name string) *ReadRoomPlayer {
	for i := 0; i < len(this.roomPlayers); i++ {
		if this.roomPlayers[i].GetUserName() == name {
			return this.roomPlayers[i]
		}
	}
	return nil
}
func (this *ReadyRoom) Update() {
	if this.IsStart == false && time.Now().Unix() > this.StartReadyTime+READYROOM_TIME {
		this.PlayersToGame()
	}
}

func (this *ReadyRoom) IsPlayersReady() bool {
	for i := 0; i < len(this.roomPlayers); i++ {
		if this.roomPlayers[i].GetIsReady() == false {
			return false
		}
	}
	return true
}

func (this *GameHall) SynRoomPlayerState_Halder(msg *Message) {
	synData := CS_SynRoomPlayerState{}
	proto.Unmarshal(msg.Data, &synData)
	room := this.GetReadyRoom(synData.GetRoomID())
	if room == nil {
		return
	}
	roomPlayer := room.GetPlayer(synData.GetUserName())
	if roomPlayer != nil {
		roomPlayer.IsReady = proto.Bool(synData.GetIsReady())
		roomPlayer.SelectId = proto.String(synData.GetSelectId())
		if room.IsPlayersReady() {
			room.PlayersToGame()
		}
		room.SynRoomPlayerState(synData.GetCamp())
	}
}

func (this *ReadyRoom) SynRoomPlayerState(camp int32) {
	playerState := &SC_SynPlayerState{}
	playerState.PlayerList = make([]*ReadRoomPlayer, this.RoomSize/2)
	playerState.RoomID = proto.Int64(this.RoomID)

	if camp == 0 {
		for i := 0; i < this.RoomSize/2; i++ {
			playerState.PlayerList = this.roomPlayers[:this.RoomSize/2]
			this.clientPlayers[i].Sess.SendPacket(PBID_SC_SynPlayerState, playerState)
		}
	} else if camp == 1 {
		for i := this.RoomSize / 2; i < this.RoomSize; i++ {
			playerState.PlayerList = this.roomPlayers[this.RoomSize/2:]
			this.clientPlayers[i].Sess.SendPacket(PBID_SC_SynPlayerState, playerState)
		}
	}
}
func (this *ReadyRoom) PlayersToGame() {
	for i := 0; i < len(this.clientPlayers); i++ {
		this.clientPlayers[i].PlayerState = PLAYERSTATE_SCENE
		this.clientPlayers[i].Sess.SetAliveTime(int64(time.Second * 60))
		GetGameHall().RemovePlayer(this.clientPlayers[i])
	}
	GetGameServer().AddMessage(CmdMessage(CMD_RunNewScene, "Clients", this.clientPlayers, "Infos", this.roomPlayers))
	GetGameHall().RemoveReadyRoom(this.RoomID)
}

//有某个玩家掉线时摧毁这个房间 大家都返回大厅
func (this *ReadyRoom) Destory() {
	GetGameHall().RemoveReadyRoom(this.RoomID)
	backtoHallData := &SC_BackToGameHall{}
	for i := 0; i < len(this.clientPlayers); i++ {
		this.clientPlayers[i].Sess.SendPacket(PBID_SC_BackToGameHall, backtoHallData)
		this.clientPlayers[i].PlayerState = PLAYERSTATE_Hall
	}
}
